package demo;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;



/**
 * 
 * @Description: 索引
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-high.html
 * https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
 *
 */
public class CreateIndexDemo {

	public static void main(String[] args) {
        createIndex();
        //deleteIndex();
    }

	/**
	 * 创建索引
	 */
	public static void createIndex() {
		try (RestHighLevelClient client = InitUtil.getClient();) {

            // 1、创建 创建索引request
            CreateIndexRequest request = new CreateIndexRequest(InitUtil.INDEX);       

            // 2、设置索引的settings
            request.settings(Settings.builder().put("index.number_of_shards", 3) // 分片数
                    .put("index.number_of_replicas", 1) // 副本数
                   // .put("analysis.analyzer.default.tokenizer", "ik_smart") // 默认分词器
            );
            
            String source = FileUtils.readFileToString(new File("src/main/resources/conf/mapping.json"),"UTF-8");
                       
            // 3、设置索引的mappings
            request.mapping(InitUtil.TYPE,source,XContentType.JSON);
            
            // 4、 设置索引的别名
            request.alias(new Alias("test_alias"));
            
            
            // 5、 发送请求         
            CreateIndexResponse createIndexResponse = client.indices().create(request,RequestOptions.DEFAULT);

            // 6、处理响应
            boolean acknowledged = createIndexResponse.isAcknowledged();
            boolean shardsAcknowledged = createIndexResponse
                    .isShardsAcknowledged();
            System.out.println("acknowledged = " + acknowledged);
            System.out.println("shardsAcknowledged = " + shardsAcknowledged);          
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * 删除索引
	 */
	public static void deleteIndex() {
		try (RestHighLevelClient client = InitUtil.getClient();) {
       
            DeleteIndexRequest request = new DeleteIndexRequest(InitUtil.INDEX);                  
            DeleteIndexResponse response = client.indices().delete(request,RequestOptions.DEFAULT);
                
            boolean acknowledged = response.isAcknowledged();         
            System.out.println("acknowledged = " + acknowledged);       
        } catch (IOException e) {
            e.printStackTrace();
        }
	}


}
